//闪电买购买物品类型交互

$(".buy_type li").on('click', function() {
    var buyTypeStr = "";
    $(".buy_type li span").css('visibility', 'hidden');
    $(this).children("span").css("visibility", "visible");
    var buyType = $(this).children("i").attr("class").split(" ")[1];
    var buyName = $(this).find(".name").html();
    buyTypeStr += "<i class='iconfont " + buyType + "'></i><div class='name'>" + buyName + "</div>";
    $(".buy_change").html("");
    $(".buy_change").html(buyTypeStr);

})

//选中动画框
$('.buy_type  li').on('click', function() {
    var $after = '<div class="after"></div>';
    var $before = '<div class="before"></div>';
    var $index = $(this).index();
    var $parent = $(this).parent('.flex_row');
    if ($parent.find('li').eq($index).find('div').hasClass('after') || $parent.find('li').eq($index).find('div').hasClass('before')) {
        return false
    } else {
        $('.buy_type li').children('.before').remove();
        $('.buy_type li').children('.after').remove(); //重置样式
        $parent.find('li').eq($index).append($after);
        $parent.find('li').eq($index).prepend($before);
        $parent.find('li').eq($index).children('.before').animate({
            "width": "75%",
            "height": "75%"
        }, 800)
        $parent.find('li').eq($index).children('.after').animate({
            "height": "75%",
            "width": "75%"
        }, 800)
    }
})

// //时间选择器控件  iscroll5 + jq
var myScroll;
var today = new Date();
var nowYear = today.getFullYear();
var nowMonth = today.getMonth() + 1;
var nowDay = today.getDate();
var nowHour = today.getHours() + 1;
var nowMinute = today.getMinutes();
// var nowTime = nowYear + "-" + nowMonth + "-" + nowDay + " " + (nowHour < 10 ? '0' : '') + nowHour + ':' + (nowMinute < 10 ? '0' : '') + nowMinute;
var nowTime = "";
//分钟范围
if (nowMinute > 55) {
    nowHour = nowHour + 1;
    nowMinute = 0;
}
//小时范围
if (nowHour > 23) {
    nowHour = 0;
    nowMinute = 0;
};
var reMin = Math.ceil(nowMinute / 5),
    reHour = nowHour,
    reDay = 0;
var dayIndex = 0,
    hourIndex = nowHour;
var minIndex = Math.ceil(nowMinute / 5);
//字符串
var orderDay = "今天",
    orderHour = (nowHour < 10 ? '0' : '') + nowHour,
    orderMinute = (minIndex == 0 ? '0' : '') + minIndex * 5,
    timeStr = "";

function getNowTime(count) {
    var date = new Date();
    date.setDate(date.getDate() + count)
    var thisYear = date.getFullYear();
    var thisMonth = date.getMonth() + 1;
    var thisDay = date.getDate();
    return nowTime = thisYear + "-" + thisMonth + "-" + thisDay + " " + orderHour + ':' + orderMinute;
}
//取消按钮文本
var cancelTxt = $("#select_time").html().substring(0, 4);
$('.time_box .cancel').html(cancelTxt);
$("#select_time").on('click', function() {
    //刷新时间
    $('.mask').css('display', 'block'); //遮罩层出现	
    $('.time_box').css('display', 'block');
    var dayScroll = new IScroll('#time_day', {
        scrollY: true,
        listenY: true,
        tap: true,
        momentum: true,
        preventDefault: false,
        snap: 'li'
    });
    var hourScroll = new IScroll('#time_hour', {
        scrollY: true,
        listenY: true,
        momentum: true,
        preventDefault: false,
        tap: true,
        snap: 'li'
    });
    var minuteScroll = new IScroll('#time_minute', {
        scrollY: true,
        listenY: true,
        momentum: true,
        preventDefault: false,
        tap: true,
        snap: 'li'
    });
    //根据当前时间重置时间列表
    var liHeight = parseInt($("#time_hour li").css("height"));

    function rewriteList() {
        //无问题；
        //判断是否为当前时间；列表的第一个	
        dayIndex = 0;
        hourIndex = nowHour;
        minIndex = reMin;
        $("#time_day li").removeClass('li_select').eq(0).addClass('li_select');
        dayScroll.refresh();
        dayScroll.goToPage(0, 0);
        $('#time_hour li').css("display", "block").slice(0, reHour).css("display", "none");
        $('#time_hour li').removeClass('li_select').eq(reHour).addClass('li_select');
        hourScroll.refresh();
        hourScroll.goToPage(0, 0);
        $('#time_minute li').css("display", "block").slice(0, reMin).css("display", "none");
        $("#time_minute li").removeClass('li_select').eq(reMin).addClass('li_select');
        minuteScroll.refresh();
        minuteScroll.goToPage(0, 0);
        orderDay = "今天";
        orderHour = (nowHour < 10 ? "0" : "") + nowHour;
        orderMinute = orderMinute = (reMin == 0 ? "0" : "") + reMin * 5;
    }
    rewriteList(); //执行重置
    //日期滑动结束函数
    dayScroll.on('scrollEnd', function(e) {
        dayHeight = Math.abs(this.y);
        var maxLen = $("#time_day li").length - 1;
        dayIndex = Math.ceil(dayHeight / liHeight);
        if (dayIndex >= maxLen) {
            //console.log("dayIndex>maxLen" + dayIndex);
            dayIndex = maxLen - 1;

        }
        var isToday = (dayIndex <= 0);
        var isNowHour = (hourIndex == nowHour);
        $("#time_day li").removeClass('li_select').eq(dayIndex).addClass('li_select');
        if (isToday) {
            $("#time_hour li").css("display", "block").slice(0, reHour).css("display", "none");
            $('#time_hour li').removeClass('li_select').eq(reHour).addClass('li_select');
            hourScroll.refresh();
            hourScroll.goToPage(0, reHour);
            minIndex = reMin;
            $("#time_minute li").css("display", "block").slice(0, reMin).css("display", "none");
            $('#time_minute li').removeClass('li_select').eq(reMin).addClass('li_select');
            minuteScroll.refresh();
            minuteScroll.goToPage(0, reMin);
        } else {
            $('#time_hour li').css("display", "block").removeClass('li_select').eq(0).addClass('li_select');
            hourScroll.refresh();
            hourScroll.goToPage(0, 0);
            $('#time_minute li').css("display", "block").removeClass('li_select').eq(0).addClass('li_select');
            hourIndex = 0;
            minIndex = 0;
            minuteScroll.goToPage(0, 0);
        }
        orderDay = $("#time_day .li_select").html(); //当前天数字符串；
        orderHour = $("#time_hour .li_select").html();
        orderMinute = $("#time_minute .li_select").html();
        // event.stopPropagation();
        // event.preventDefault();
    })
    hourScroll.on('scrollEnd', function() {
            //顺序不能变
            var hourHeight = Math.abs(this.y);
            var maxLen = $("#time_hour li").length - 1;
            hourIndex = Math.ceil(hourHeight / liHeight);
            var isToday = (dayIndex <= 0);
            if (isToday) {
                hourIndex = Math.ceil(hourHeight / liHeight) + nowHour;
            } else {
                hourIndex = Math.ceil(hourHeight / liHeight);
            }

            if (hourIndex >= maxLen) {
                hourIndex = maxLen - 1;
            }
            $("#time_hour li").removeClass('li_select').eq(hourIndex).addClass('li_select');
            hourScroll.refresh();
            hourScroll.goToPage(0, hourIndex);
            var isNowHour = (hourIndex == nowHour);
            if (isToday && isNowHour) {
                $('#time_minute li').css("display", "block").slice(0, reMin).css("display", "none");
                $('#time_minute li').removeClass('li_select').eq(reMin).addClass('li_select');
                minuteScroll.refresh();
                minuteScroll.goToPage(0, 0);
            } else {
                $('#time_minute li').css("display", "block").removeClass('li_select').eq(0).addClass('li_select');
                minuteScroll.refresh();
                minuteScroll.goToPage(0, 0);
            }
            orderDay = $("#time_day .li_select").html(); //当前天数字符串；
            orderHour = $("#time_hour .li_select").html();
            orderMinute = $("#time_minute .li_select").html();
        })
        //分钟滑动结束函数
    minuteScroll.on('scrollEnd', function() {
            var minuteHeight = Math.abs(this.y);
            minIndex = Math.ceil(minuteHeight / liHeight);
            var isToday = (dayIndex <= 0);
            var isNowHour = (hourIndex == nowHour);
            if (isToday && isNowHour) {
                minIndex = Math.ceil(minuteHeight / liHeight) + Math.ceil(nowMinute / 5);
            } else {
                minIndex = Math.ceil(minuteHeight / liHeight);
            }
            $("#time_minute li").removeClass('li_select').eq(minIndex).addClass('li_select');
            orderDay = $("#time_day .li_select").html(); //当前天数字符串；
            orderHour = $("#time_hour .li_select").html();
            orderMinute = $("#time_minute .li_select").html();

        })
        //day列表点击效果
    $('#time_day li').off("tap").on('tap', tapDay);

    function tapDay() {
        var maxLen = $("#time_day li").length - 1;
        dayIndex = $(this).index();
        if (dayIndex == maxLen) {
            dayIndex = maxLen - 1;
        }
        $('#time_day li').removeClass('li_select').eq(dayIndex).addClass('li_select');
        dayScroll.goToPage(0, dayIndex);
    }
    //hour列表点击效果
    $('#time_hour li').off("tap").on('tap', tapHour);

    function tapHour() {
        var maxLen = $("#time_hour li").length - 1;
        hourIndex = $(this).index();
        if (hourIndex == maxLen) {
            hourIndex = maxLen - 1;
        }
        $('#time_hour li').removeClass('li_select').eq(hourIndex).addClass('li_select');
        hourScroll.goToPage(0, hourIndex);
    }
    //minute列表点击效果
    $('#time_minute li').off("tap").on('tap', tapMinute);

    function tapMinute() {
        var maxLen = $("#time_minute li").length - 1;
        minIndex = $(this).index();
        if (minIndex == maxLen) {
            minIndex = maxLen - 1;
        }
        $('#time_minute li').removeClass('li_select').eq(minIndex).addClass('li_select');
        minuteScroll.goToPage(0, minIndex);
    }
    // 解决卡顿问题
    document.addEventListener('touchmove', function(e) {
        e.preventDefault();
    }, false);
    var refreshScroll = function refreshMyScroll() {
        dayScroll.refresh();
        hourScroll.refresh();
        minuteScroll.refresh();
    }
    setTimeout(refreshScroll, 0);
    //销毁myScroll
    function destroyMyScroll() {
        clearTimeout(refreshScroll);
        hourScroll.destroy();
        hourScroll = null;
        minuteScroll.destroy();
        minuteScroll = null;
        dayScroll.destroy();
        dayScroll = null;
    }
    //事件嵌套事件会叠加 需要解除绑定事件 on对应off bind对应unbind
    $(".time_box .confirm").off('click').on("click", function(e) {
        $(".mask").css("display", "none");
        $(".time_box").css('display', "none");
        timeStr = orderDay + "  " + orderHour + ":" + orderMinute;
        $("#select_time").html(timeStr);
        $(".time_box").unbind("click");
        if (orderDay == "今天") {
            getNowTime(0);
            //console.log(nowTime);
        } else if (orderDay == "明天") {
            getNowTime(1);
            //console.log(nowTime);
        } else if (orderDay == "后天") {
            getNowTime(2);
            //console.log(nowTime);
        };
        //console.log(timeStr);

        destroyMyScroll();
        event.stopPropagation();
    })
    $(".time_box .cancel").off('click').on("click", function() {
            $(".mask").css("display", "none");
            $(".time_box").css('display', "none");
            destroyMyScroll();
        })
        // event.stopPropagation();
});
//商品信息交互优化 2017-6-20 Desc
var Desc = sessionStorage.getItem("Desc");
$("#product_mes").val(Desc);
$("#product_mes").blur(function() {
    sessionStorage.setItem("Desc", $("#product_mes").val());
})